//
//  PostWithHeaer.m
//  WHYNetworkTool
//
//  Created by 王宏洋 on 15/11/7.
//  Copyright © 2015年 王宏洋. All rights reserved.
//

#import "PostWithHeaderAPI.h"

@implementation PostWithHeaderAPI
{
    NSDictionary *_dic;
    NSString *_urlstr;
    MBProgressHUD *hud;
    NSDictionary *headerDictory;
}
- (id)initWith:(NSDictionary *)bodyDic urlStr:(NSString *)urlstr header:(NSDictionary *)headerdic
{
    if (self = [super init]) {
        _dic = bodyDic;
        _urlstr = urlstr;
        headerDictory = headerdic;
            }
    return self;
   
}
- (id)initWith:(NSDictionary *)bodyDic urlStr:(NSString *)urlstr
{
    if (self = [super init]) {
        _dic = bodyDic;
        _urlstr = urlstr;
           }
    return self;
}

- (NSTimeInterval)requestTimeoutInterval
{
    // 超时设置
    return 15;
}

- (NSDictionary *)requestHeaderFieldValueDictionary
{
    
    return headerDictory;
}

- (NSString *)requestUrl
{
    return _urlstr;
}

- (id)requestArgument {
    
    
    return _dic;
}

- (YTKRequestMethod)requestMethod
{
    return YTKRequestMethodPost;
}




- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"fractionCompleted"])
    {
        hud.progress = 1;
        [hud hide:YES afterDelay:2];
    }
    else
    {
        [super observeValueForKeyPath:keyPath ofObject:object
                               change:change context:context];
    }
}

- (NSDictionary *)responseDic {
    
    NSData *data = [self responseData];
    
    NSProgress *progress = [NSProgress progressWithTotalUnitCount:data.length];
    [progress addObserver:self forKeyPath:NSStringFromSelector(@selector(fractionCompleted)) options:NSKeyValueObservingOptionInitial context:nil];
    
    return  [self responseBody];
    
}
@end
